package rs.networx.servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class GuestSearchServlet extends HttpServlet {

	private static final long serialVersionUID = -4475140855914183334L;

	@EJB
	private PersonDao personDao;

	private String searchData;

	private List<Person> searchResults;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		System.out.println("LOADED SERVLET: GuestSearchServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		//HttpSession session = request.getSession(true);

		searchData = request.getParameter("SearchText");
		System.out.println(searchData);

		if (searchData != null && searchData != "") {
			searchResults = personDao.search(searchData);
			System.out.println(searchResults);
			if (searchResults == null || searchResults.size() == 0) {
				request.setAttribute("searchStatus",
						ErrorMessages.NO_SEARCH_RESULTS);
			} else if (searchResults != null && searchResults.size() > 0) {
				request.setAttribute("searchStatus",
						ErrorMessages.THERE_IS_SEARCH_RESULTS);
				request.setAttribute("guestSearchResults", searchResults);
				
			}
			request.getRequestDispatcher("GuestHomePage.jsp").forward(
					request, response);
			return;
		}
	}

	// --------------------------------------------------------------------------------------------------------------------------------------------------
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}

}
